package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * redis缓存逻辑
 * 1 insert  清理单个dish_
 * 2 单个删除/多个删除  清理所有dish_
 * 3 修改   清理所有dish_
 */

@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@Slf4j
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据Id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据Id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 保存菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("保存菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("dish:{}", dishDTO.toString());
        dishService.save(dishDTO);
        // 清除redis缓存
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 修改菜品数据
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品数据")
    @PutMapping
    public Result update( @RequestBody DishDTO dishDTO ){
        dishService.updateById(dishDTO);
        // 清除redis缓存
        // 清除redis缓存  清除所有dish_ 开头的缓存
        Set keys = redisTemplate.keys("dish_*");// 获取所有dish_ 数据
        redisTemplate.delete(keys);
        return  Result.success();
    }

    /**
     * 根据Id 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result removeByIds( @RequestParam List<Long> ids ){
         dishService.removeByIds(ids);
        // 清除redis缓存  清除所有dish_ 开头的缓存
        Set keys = redisTemplate.keys("dish_*");// 获取所有dish_ 数据
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据Id的  更新菜品起售 停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更新菜品起售 停售")
    @PostMapping("/status/{status}")
    public Result UpdateStatus(@PathVariable Integer status, Long id){
        dishService.updateStatus(status, id);
        // 清除redis缓存  清除所有dish_ 开头的缓存
        Set keys = redisTemplate.keys("dish_*");// 获取所有dish_ 数据
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据分类Id查询商品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类Id查询商品")
    @GetMapping("/list")
    public Result<List<DishVO>> getByCategoryId(Long categoryId){
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        List<DishVO> dishVOList = dishService.listWithFlavor(dish);
        return Result.success(dishVOList);
    }

}
